package com.cn.aam.checaiduo.ui.homepage.pay;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cn.aam.checaiduo.R;
import com.cn.aam.checaiduo.base.BaseActivity;
import com.cn.aam.checaiduo.base.ToolBarManager;
import com.cn.aam.checaiduo.bean.me.VoucherB;
import com.cn.aam.checaiduo.ui.homepage.adapter.ChooseVoucherAdapter;
import com.cn.aam.checaiduo.widget.MultiStateView;

import butterknife.BindView;

import static com.cn.aam.checaiduo.Const.REQUEST_SUCCESS;

/**
 * monker on 2017/6/13.
 * 选择理财券界面
 */

public class ChooseVoucherActivity extends BaseActivity {
    public static final int RATE_COUPON = 1;//加息券
    public static final int CASH_COUPON = 2;//代金券
    private ChooseVoucherAdapter adapter;
    private int type, chooseIndex;
    private VoucherB voucherB;
    @BindView(R.id.voucher_recycler)
    RecyclerView voucherRecycler;

    @BindView(R.id.multiStateView)
    MultiStateView multiStateView;

    public static Intent newInstance(Context context, VoucherB bean, int chooseIndex, int type, String investAmount) {
        Intent intent = new Intent(context, ChooseVoucherActivity.class);
        intent.putExtra("type", type);
        intent.putExtra("chooseIndex", chooseIndex);
        intent.putExtra("investAmount", investAmount);
        intent.putExtra("bean", bean);
        return intent;
    }

    @Override
    protected int layoutId() {
        return R.layout.activity_choose_voucher;
    }

    @Override
    protected void initView() {
        type = getIntent().getIntExtra("type", -1);
        voucherB = (VoucherB) getIntent().getSerializableExtra("bean");
        chooseIndex = getIntent().getIntExtra("chooseIndex", -1);
        String investAmount = getIntent().getStringExtra("investAmount");
        String title = "";
        if (type == CASH_COUPON) {
            title = "代金券";
        } else {
            title = "加息券";
        }

        ToolBarManager.with(this, WMContent)
                .setTitle(title)
                .setLeftMenuIconRes(R.drawable.ic_nav_arrow_white)
                .setMenuTextContent("确定")
                .setMenuTextClick(new ToolBarManager.OnMenuTextClickListener() {
                    @Override
                    public void onMenuTextClick() {
                        returnNewResult();
                    }
                })
                .setLeftMenuIconClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        returnBack();
                    }
                });

        if (type == CASH_COUPON) {
            if (voucherB.getCash_coupon().size() == 0) {
                multiStateView.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                return;
            }
        } else {
            if (voucherB.getRate_coupon().size() == 0) {
                multiStateView.setViewState(MultiStateView.VIEW_STATE_EMPTY);
                return;
            }
        }

        adapter = new ChooseVoucherAdapter(voucherB, type);
        adapter.setInvestAmount(investAmount);
        adapter.setChoose(chooseIndex);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        voucherRecycler.setLayoutManager(layoutManager);
        voucherRecycler.setAdapter(adapter);
    }

    @Override
    public void onBackPressed() {
        returnBack();
    }

    private void returnNewResult() {
        Intent intent = new Intent();
        chooseIndex = adapter.getChooseItem();
        intent.putExtra("chooseIndex", chooseIndex);
        setResult(REQUEST_SUCCESS, intent);
        finish();
        overridePendingTransition(0, R.anim.slide_right_out);
    }

    private void returnBack() {
        Intent intent = new Intent();
        intent.putExtra("chooseIndex", chooseIndex);
        setResult(REQUEST_SUCCESS, intent);
        finish();
        overridePendingTransition(0, R.anim.slide_right_out);
    }
}
